/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.service;

import java.util.List;

import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.dao.CategoryDao;
import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.sys.dao.OfficeDao;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.cms.entity.CmsBusiness;
import com.thinkgem.jeesite.modules.cms.dao.CmsBusinessDao;

/**
 * 商家信息录入展示Service
 * @author 张朋
 * @version 2016-06-16
 */
@Service
@Transactional(readOnly = true)
public class CmsBusinessService extends CrudService<CmsBusinessDao, CmsBusiness> {

    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private CmsBusinessDao cmsBusinessDao;

    @Autowired
    private OfficeDao officeDao;

	public CmsBusiness get(String id) {
		return super.get(id);
	}
	
	public List<CmsBusiness> findList(CmsBusiness cmsBusiness) {
		return super.findList(cmsBusiness);
	}
	
	public Page<CmsBusiness> findPage(Page<CmsBusiness> page, CmsBusiness cmsBusiness) {
        if (cmsBusiness.getCategory()!=null && StringUtils.isNotBlank(cmsBusiness.getCategory().getId()) && !Category.isRoot(cmsBusiness.getCategory().getId())){
            Category category = categoryDao.get(cmsBusiness.getCategory().getId());
            if (category==null){
                category = new Category();
            }
            category.setParentIds(category.getId());
            category.setSite(category.getSite());
            cmsBusiness.setCategory(category);
        }
        else{
            cmsBusiness.setCategory(new Category());
        }
		return super.findPage(page, cmsBusiness);
	}

    public Page<CmsBusiness> findBusinessPage(Page<CmsBusiness> page, CmsBusiness cmsBusiness) {
//        if (cmsBusiness.getCategory()!=null && StringUtils.isNotBlank(cmsBusiness.getCategory().getId()) && !Category.isRoot(cmsBusiness.getCategory().getId())){
//            Category category = categoryDao.get(cmsBusiness.getCategory().getId());
//            if (category==null){
//                category = new Category();
//            }
//            category.setParentIds(category.getId());
//            category.setSite(category.getSite());
//            cmsBusiness.setCategory(category);
//        }
//        else{
//            cmsBusiness.setCategory(new Category());
//        }

        cmsBusiness.setPage(page);
        page.setList(cmsBusinessDao.findBusinessList(cmsBusiness));
        return page;
//        return .(page, cmsBusiness);
    }
	
	@Transactional(readOnly = false)
	public void save(CmsBusiness cmsBusiness) {
		if(cmsBusiness.getIntroduce() != null){
            cmsBusiness.setIntroduce((StringEscapeUtils.unescapeHtml4(cmsBusiness.getIntroduce())));
        }
        super.save(cmsBusiness);
	}
	
	@Transactional(readOnly = false)
	public void delete(CmsBusiness cmsBusiness) {
		super.delete(cmsBusiness);
	}
	
}